<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2017/12/29
 * Time: 18:23
 */

namespace App;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    use SoftDeletes;
    // 表名称
    protected $table = 'product';
    // 开启自动时间戳
    public $timestamps = true;
    // 日期都存储为时间戳
    protected $dateFormat = 'U';

    // 将运费改变面额单位从元换算成分
    public function setCarriageAttribute($value)
    {
        $this->attributes['carriage'] = $value*100;
    }
    // 将运费改变面额单位从分换算成元
    public function getCarriageAttribute($value)
    {
        return $value/100;
    }

    public function productContent()
    {
        return $this->hasMany('App\ProductContent', 'product_id', 'id');
    }

    public function tag()
    {
        return $this->belongsToMany('App\Tag', 'product_mapping_tag', 'product_id', 'tag_id');
    }

    public function category()
    {
        return $this->belongsTo('App\Category', 'category_id', 'id');
    }

    public function goods()
    {
        return $this->hasMany('App\Goods', 'product_id', 'id');
    }

    public function productPhoto()
    {
        return $this->hasMany('App\ProductPhoto', 'product_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo('App\Users', 'user_id', 'id');
    }

    public function cafe()
    {
        return $this->belongsTo('App\Cafe', 'cafe_id', 'id');
    }

    public function partner()
    {
        return $this->belongsTo('App\Partner', 'partner_id', 'id');
    }
}